﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder;
    using CKFinder.Connector;
    using System;
    using System.IO;
    using System.Web;

    internal class ThumbnailCommandHandler : CommandHandlerBase
    {
        public override void SendResponse(HttpResponse response)
        {
            base.CheckConnector();
            try
            {
                base.CheckRequest();
            }
            catch (ConnectorException exception)
            {
                response.AddHeader("X-CKFinder-Error", exception.Number.ToString());
                response.StatusCode = 0x193;
                response.End();
                return;
            }
            catch
            {
                response.AddHeader("X-CKFinder-Error", 110.ToString());
                response.StatusCode = 0x193;
                response.End();
                return;
            }
            if (Config.Current.Thumbnails.Enabled)
            {
                if (!base.CurrentFolder.CheckAcl(AccessControlRules.FileView))
                {
                    response.AddHeader("X-CKFinder-Error", 0x67.ToString());
                    response.StatusCode = 0x193;
                    response.End();
                }
                else
                {
                    bool flag = false;
                    string str = HttpContext.Current.Request["FileName"];
                    string path = Path.Combine(base.CurrentFolder.ThumbsServerPath, str);
                    if (!CKFinder.Connector.Connector.CheckFileName(str))
                    {
                        response.AddHeader("X-CKFinder-Error", 0x6d.ToString());
                        response.StatusCode = 0x193;
                        response.End();
                    }
                    else if (Config.Current.CheckIsHiddenFile(str))
                    {
                        response.AddHeader("X-CKFinder-Error", 0x75.ToString() + " - Hidden folder");
                        response.StatusCode = 0x194;
                        response.End();
                    }
                    else
                    {
                        if (!File.Exists(path))
                        {
                            string str3 = Path.Combine(base.CurrentFolder.ServerPath, str);
                            if (!File.Exists(str3))
                            {
                                response.AddHeader("X-CKFinder-Error", 0x75.ToString());
                                response.StatusCode = 0x194;
                                response.End();
                                return;
                            }
                            ImageTools.ResizeImage(str3, path, Config.Current.Thumbnails.MaxWidth, Config.Current.Thumbnails.MaxHeight, true, Config.Current.Thumbnails.Quality);
                        }
                        FileInfo info = new FileInfo(path);
                        string etag = info.LastWriteTime.Ticks.ToString("X") + "-" + info.Length.ToString("X");
                        string str5 = base.Request.ServerVariables["HTTP_IF_NONE_MATCH"];
                        if (((str5 != null) && (str5.Length > 0)) && (etag == str5))
                        {
                            flag = true;
                        }
                        if (!flag)
                        {
                            string s = base.Request.ServerVariables["HTTP_IF_MODIFIED_SINCE"];
                            if ((s != null) && (s.Length > 0))
                            {
                                try
                                {
                                    if (DateTime.Parse(s) >= info.LastWriteTime)
                                    {
                                        flag = true;
                                    }
                                }
                                catch
                                {
                                    flag = false;
                                }
                            }
                        }
                        if (flag)
                        {
                            response.StatusCode = 0x130;
                            response.End();
                        }
                        else
                        {
                            string str7 = Path.GetExtension(path).TrimStart(new char[] { '.' }).ToLower();
                            if (path == ".jpg")
                            {
                                response.ContentType = "image/jpeg";
                            }
                            else
                            {
                                response.ContentType = "image/" + str7;
                            }
                            response.Cache.SetETag(etag);
                            response.Cache.SetLastModified(info.LastWriteTime);
                            response.Cache.SetCacheability(HttpCacheability.Private);
                            response.WriteFile(path);
                        }
                    }
                }
            }
            else
            {
                response.AddHeader("X-CKFinder-Error", 0x1f5.ToString());
                response.StatusCode = 0x193;
                response.End();
            }
        }
    }
}

